// pages/detail/detail.jsgf  
var common = require("../../utils/public.js") //引入public.js脚本
const api = require('../../utils/api.js')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    opacity: 1,
    //商品详情
    goodsDetails: [],
    //商品信息
    goodsMess: '',
    //轮播图
    swiperImage: [],
    // 详情图
    detailImage: [],
    //详情id
    getFraction: "",
    login: true,
    masking: 'masking',
    current: 0,
    // 轮播end
    toView: 'productBox',
    navArray: [{
      name: '商品',
      hash: 'productBox'
    }, {
      name: '详情',
      hash: 'infoBox'
    }],
    collect: null,
    goodsPic: '',
    shareImg: '',
    imgs: [],
    fromShare: '',
    fenid: null
  },
  // -----------------------------------------自定义函数-------------------------------------
  enlargeImg: function (event) {
    var src = event.currentTarget.dataset.src; //获取data-src 
    var imgList = event.currentTarget.dataset.list; //获取data-list 
    var arr = []
    imgList.forEach(function (v) {
      arr.push(v.url)
    })
    //图片预览 
    wx.previewImage({
      current: src, // 当前显示图片的http链接 
      urls: arr // 需要预览的图片http链接列表 
    })
  },

  // 锚点定位
  toViewClick: common.throttleFunc(function (e) {
    this.setData({
      toView: e.target.dataset.hash
    })
  }),

  // 自定义轮播indicator-dots样式
  swiperChange: function (e) {
    var that = this;
    if (e.detail.source == 'touch' || e.detail.source == 'autoplay') {
      that.setData({
        current: e.detail.current
      })
    }
  },
  //显示商品描述弹出框
  openDes: common.throttleFunc(function (e) {
    this.setData({
      showModalStatus: 'on',
      desStatus: 'on'
    })
  }),
  // 返回上一层
  onBack: function () {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  //隐藏弹出框
  hideModal: common.throttleFunc(function (e) {
    this.setData({
      showModalStatus: '',
      desStatus: '',
      actStatus: ''
    })
  }),
  handleSubmit() {
    wx.redirectTo({
      url: '/pages/userMess/userMess?cityId=' + this.data.cityId + '&userId=' + this.data.fenid,
    })
  },
  // -----------------------------生命周期---------------------------------
  onLoad: function (options) {
    let that = this;
    let { cityId, id, userId, fromShare } = options
    let token = wx.getStorageSync('token')
    let type = wx.getStorageSync("type")
    let goodMess = JSON.parse(options.goodmess)
    this.setData({
      fromShare,
      cityId,
      userId,
      id,
      fenid: options.fenid
    })
    // 登录了，没注册
    if (token) {
      if (type == 2) {
        wx.redirectTo({
          url: '/pages/detail/detail?id=' + id + '&fromShare=true',
        })
      } else if (type == 1 && wx.getStorageSync('codeNum') == 99) {
        wx.redirectTo({
          url: '/pages/detail/detail?id=' + id + '&fromShare=true',
        })
      }
      else {
        wx.switchTab({
          url: '/pages/index/index',
        })
      }
    } else {
      //注册了
      if(!type){
        wx.switchTab({
          url: '/pages/index/index',
        }) 
      }
      wx.getSystemInfo({
        success: function (res) {
          let clientHeight = res.windowHeight;
          let clientWidth = res.windowWidth;
          let ratio = 750 / clientWidth;
          let height = clientHeight * ratio;
          that.setData({
            autoHeight: height - 112,
            id: options.id,
            barHeight: res.statusBarHeight,
            iconTop: res.statusBarHeight + 60,
          });
        }
      })
    }







    //商品详情
    // api.goodsDetails({
    //   cityId: wx.getStorageSync('cityId') || cityId,
    //   id: that.data.id || id,
    //   userId: wx.getStorageSync('openId') || userId,
    // }).then(res => {
    //   if (res.result == true) {
    //     let login = res.login;
    //     if (login == true) {
    //       let type = wx.getStorageSync("type")
    //       if (type == 2) {
    //         wx.navigateTo({
    //           url: '/pages/detail/detail?id=' + id,
    //         })
    //       } else if (type == 1 && wx.getStorageSync('codeNum') == 99) {
    //         wx.navigateTo({
    //           url: '/pages/detail/detail?id=' + id,
    //         })
    //       }
    //     } else {
    //       let goodsList = res.object.goodsDetailsEntityList
    //       var swiper = []
    //       var detail = []
    //       res.object.goodsPictureEntityList.forEach(function (value, index, array) {
    //         if (value.useType == 1) {
    //           if (value.url == null) {

    //           } else {
    //             swiper.push(value.url)
    //           }

    //         } else if (value.useType == 2) {
    //           if (value.url == null) {

    //           } else {
    //             detail.push(value.url)
    //           }
    //         }
    //       });
    //       that.setData({
    //         swiperImage: swiper,
    //         detailImage: detail,
    //         goodsPic: swiper[0],
    //         num: goodsList,
    //         goodsMess: res.object,
    //         masking: ''
    //       })
    //     }
    //   }
    // })

  }
})